if exists (select 1
   from dbo.sysreferences r join dbo.sysobjects o on (o.id = r.constid and o.type = 'F')
   where r.fkeyid = object_id('evale_object') and o.name = 'FK_EVALE_OB_REFERENCE_TAST')
alter table evale_object
   drop constraint FK_EVALE_OB_REFERENCE_TAST
go

if exists (select 1
   from dbo.sysreferences r join dbo.sysobjects o on (o.id = r.constid and o.type = 'F')
   where r.fkeyid = object_id('factor') and o.name = 'FK_FACTOR_REFERENCE_MODULE')
alter table factor
   drop constraint FK_FACTOR_REFERENCE_MODULE
go

if exists (select 1
   from dbo.sysreferences r join dbo.sysobjects o on (o.id = r.constid and o.type = 'F')
   where r.fkeyid = object_id('module') and o.name = 'FK_MODULE_REFERENCE_USER')
alter table module
   drop constraint FK_MODULE_REFERENCE_USER
go

if exists (select 1
   from dbo.sysreferences r join dbo.sysobjects o on (o.id = r.constid and o.type = 'F')
   where r.fkeyid = object_id('tast') and o.name = 'FK_TAST_REFERENCE_USER')
alter table tast
   drop constraint FK_TAST_REFERENCE_USER
go

if exists (select 1
            from  sysobjects
           where  id = object_id('evale_object')
            and   type = 'U')
   drop table evale_object
go

if exists (select 1
            from  sysobjects
           where  id = object_id('factor')
            and   type = 'U')
   drop table factor
go

if exists (select 1
            from  sysobjects
           where  id = object_id('module')
            and   type = 'U')
   drop table module
go

if exists (select 1
            from  sysobjects
           where  id = object_id('tast')
            and   type = 'U')
   drop table tast
go

if exists (select 1
            from  sysobjects
           where  id = object_id('"user"')
            and   type = 'U')
   drop table "user"
go

/*==============================================================*/
/* Table: evale_object                                          */
/*==============================================================*/
create table evale_object (
   evale_id             int                  not null,
   tast_id              int                  null,
   name                 varchar(255)         null,
   constraint PK_EVALE_OBJECT primary key (evale_id)
)
go

/*==============================================================*/
/* Table: factor                                                */
/*==============================================================*/
create table factor (
   fac_id               int                  not null,
   mod_id               int                  null,
   name                 varchar(255)         null,
   level                int                  null,
   pid                  int                  null,
   constraint PK_FACTOR primary key (fac_id)
)
go

/*==============================================================*/
/* Table: module                                                */
/*==============================================================*/
create table module (
   mod_id               int                  not null,
   uid                  Integer              null,
   name                 varchar(255)         null,
   type                 varchar(255)         null,
   constraint PK_MODULE primary key (mod_id)
)
go

/*==============================================================*/
/* Table: tast                                                  */
/*==============================================================*/
create table tast (
   tast_id              int                  not null,
   name                 varchar(255)         null,
   description          varchar(255)         null,
   uid                  int                  null,
   constraint PK_TAST primary key (tast_id)
)
go

/*==============================================================*/
/* Table: "user"                                                */
/*==============================================================*/
create table "user" (
   id                   Integer              not null,
   loginName            varchar(255)         null,
   password             varchar(255)         null,
   trueName             varchar(255)         null,
   sex                  varchar(255)         null,
   company              varchar(255)         null,
   headship             varchar(255)         null,
   phone                varchar(255)         null,
   email                varchar(255)         null,
   constraint PK_USER primary key (id)
)
go

alter table evale_object
   add constraint FK_EVALE_OB_REFERENCE_TAST foreign key (tast_id)
      references tast (tast_id)
go

alter table factor
   add constraint FK_FACTOR_REFERENCE_MODULE foreign key (mod_id)
      references module (mod_id)
go

alter table module
   add constraint FK_MODULE_REFERENCE_USER foreign key (uid)
      references "user" (id)
go

alter table tast
   add constraint FK_TAST_REFERENCE_USER foreign key (uid)
      references "user" (id)
go
